package com.example.module_car.adapter

import android.database.DataSetObserver
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_common.utils.ToastUtils
import com.example.module_car.databinding.GridItemBinding
import com.example.module_car.model.ProvincesEntity

class ProvincesAdapter(
    private val onItemClick: (String) -> Unit
) : RecyclerView.Adapter<ProvincesAdapter.ViewHolder>(){
    val mList = mutableListOf<ProvincesEntity>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = GridItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.text.text = mList[position].abbreviation
        holder.text.setOnClickListener {
            onItemClick(mList[position].abbreviation)
            ToastUtils.showShort(holder.binding.gridProvincesText.context, "已选择：${mList[position].abbreviation}省")
        }
    }

    fun setList(listItem : MutableList<ProvincesEntity>) {
        mList.clear()
        mList.addAll(listItem)
        notifyDataSetChanged()
    }
    inner class ViewHolder(val binding : GridItemBinding) : RecyclerView.ViewHolder(binding.root){
        val text = binding.gridProvincesText
    }
}

